package com.si.util;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

public class PaginationTag extends TagSupport{
	private static final long serialVersionUID = 1L;

	private String curPage;
	private String totalPage;
	private String pageSize;

	public int doStartTag() throws JspException {
		
	  totalPage=StringUtils.isBlank(totalPage)==false?totalPage:"0";
	  JspWriter out = pageContext.getOut();
	  if (pageSize == null || pageSize == "") {
		  pageSize = "1";
	  }
	  int pageNumber=0;
	  if (Integer.parseInt(totalPage)%Integer.parseInt(pageSize) == 0 ){ 
		  pageNumber = Integer.parseInt(totalPage)/Integer.parseInt(pageSize);
	  } else{
		  pageNumber = Integer.parseInt(totalPage)/Integer.parseInt(pageSize) + 1; 
	  }
	  if (Integer.parseInt(curPage) < 0) {
		  curPage = "1";
	  }
	  try {
		  out.print("<ul class='pagination'>");
		  
		  if(pageNumber>1){
			  if((Integer.parseInt(curPage)-1<0)){
				  out.print("<li class='disabled'><a class='paginate_button first'>首页</a></li>");
				  out.print("<li class='disabled'><a class='paginate_button previous'><< 上一页</a></li>");
			  }else{
				  out.print("<li onclick='page(0)'><a class='paginate_button first'>首页</a></li>");
				  out.print("<li onclick=page("+(Integer.parseInt(curPage) - 1)+")><a class='paginate_button previous'><< 上一页</a></li>");
			  }
			 
			  
			  int start=(Integer.parseInt(curPage))-4;
			  if(start<=0){
				  start = 0;
			  }
			  int end=7;
			  if(Integer.parseInt(curPage)-start==4){
				  start=start+1;
				  end=end+start;
				  if(end>=pageNumber){
					  end=pageNumber;
				  }
			  }
			  if(end>=pageNumber){
				  end=pageNumber;
			  }
			  for (int i = start+1; i <=end; i++) {
				    if(i-1== Integer.parseInt(curPage)){
				    	out.print("<li class='active' onclick=page("+(i-1)+")><a tabindex='0'>"+i+"</a></li>");
				    }else{
				    	out.print("<li onclick='page("+(i-1)+")'><a tabindex='0'>"+i+"</a></li>");
				    }
			  }
			  if(Integer.parseInt(curPage)>=pageNumber-1){
				  out.print("<li class='disabled'><a tabindex='0' class='paginate_button next'>下一页 >></a></li>");
			  }else{
				  out.print("<li onclick=page("+(Integer.parseInt(curPage)+1)+")><a tabindex='0' class='paginate_button next'>下一页 >></a></li>");
			  }
			  if(Integer.parseInt(curPage)>=pageNumber-1){
				  out.print("<li class='disabled'><a tabindex='0'  class='paginate_button last'>末页</a></span>");
			  }else{
				  out.print("<li onclick=page("+(pageNumber-1)+")><a tabindex='0'  class='paginate_button last'>末页</a></span>");
			  }
			  out.print("</ul>");
			  out.println("<input type=hidden id='curPage' name=curPage value='0'>");
			  out.println("<script type=\"text/javascript\">");
			  out.println("function page(n){");
			  out.println("document.getElementById(\"curPage\").value=n;");
			  out.println("document.getElementById(\"myform\").submit();");
			  out.println("}");
			  out.println("</script>");
		  }
		  
	  } catch (IOException e) {
		  e.printStackTrace();
	  }
	  return super.doStartTag();
	}

	public void setCurPage(String curPage) {
	  this.curPage = curPage;
	}

	public void setPageSize(String pageSize) {
	  this.pageSize = pageSize;
	}
	public void setTotalPage(String totalPage) {
	  this.totalPage = totalPage;
	}

}
